package com.example.lib_mvp.views;

import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.widget.EditText;

/**
 * 用于控制EditText 俩位字母 5个数字长读七位 <br/>
 *  Pattern.matches("^[A-Za-z]{2}[0-9]{5}$", s)
 * @author tfj
 */
public class LettersAndNumbersTextWatcher implements TextWatcher {

    EditText editText;

    public LettersAndNumbersTextWatcher(EditText editText) {
        this.editText = editText;
        editText.setFilters(new InputFilter[]{new InputFilter.AllCaps()});
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence s, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (s.length() == 0) {
            return;
        }
        if (s.length() > 18) {
            editText.setText(s.subSequence(0, 18));
            editText.setSelection(editText.length());
            return;
        }
       /* for (int i = 0; i < s.length(); i++) {
            String str = s.subSequence(i, i + 1).toString();
            if (i <= 1) {
                if (!str.matches("^[A-Za-z]$")) {
                    editText.setText(""); // 清空输入，或者显示错误提示信息给用户
                }
            } else {
                if (!str.matches("^[0-9]$")) {
                    editText.setText(s.subSequence(0, i)); // 清空输入，或者显示错误提示信息给用户
                    editText.setSelection(editText.length());
                }
            }
        }*/
    }
}